package com.zero.mms.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.util.JsonCreator;
import com.zero.mms.model.SalaryItem;
import com.zero.youxiao.service.SalaryItemService;

@Controller("salaryItemController")
@RequestMapping("salaryItem")
public class SalaryItemController {

	@Inject
	private SalaryItemService salaryItemService;

	@RequestMapping("/search")
	@ResponseBody
	public Object searchItems(String itemName, String itemType) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		System.out.println(itemName);
		try {
			List<SalaryItem> salaryItems = salaryItemService.searchSalaryItems(itemName, itemType);
			objectNode.putPOJO("response", salaryItems);
			objectNode.put("message", "获取部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/searchByPage")
	@ResponseBody
	public Object searchItemsBypage(String itemName, String itemType) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		System.out.println(itemName);
		try {
			Pager<SalaryItem> salaryItems = salaryItemService.searchSalaryItemsByPage(itemName, itemType);
			objectNode.putPOJO("response", salaryItems);
			objectNode.put("message", "获取部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Object addItem(String itemName, String itemType, Integer lastOrder,
			Integer itemOrder, String itemAdd, String itemShow,
			String itemMemo, Integer itemCalItemId, String itemCalPar1,
			String itemOp, String itemCalPar2) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			salaryItemService.addSalaryItem(itemName, itemType, lastOrder,
					itemOrder, itemAdd, itemShow, itemMemo, itemCalItemId,
					itemCalPar1, itemOp, itemCalPar2);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "添加工资项目成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/loadItem")
	@ResponseBody
	public Object loadItem(Integer itemId) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			SalaryItem salaryItem = salaryItemService.loadItem(itemId);
			objectNode.putPOJO("response", salaryItem);
			objectNode.put("message", "获取工资项目信息成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Object updateItem(Integer itemId, String itemName, String itemType,
			 String itemAdd, String itemMemo,String itemShow,String itemOrder,String itemCal) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			salaryItemService.updateSalaryItem(itemId, itemName, itemType,itemAdd,itemMemo,itemShow,itemOrder,itemCal);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "更新工资项目成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}
	
	@RequestMapping("/updateCal")
	@ResponseBody
	public Object updateItemCal(Integer itemId,String itemCal) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			salaryItemService.updateSalaryItem(itemId,itemCal);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "更新工资项目成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteItem(Integer itemId) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			salaryItemService.deleteItem(itemId);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "删除工资项目成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/getOrder")
	@ResponseBody
	public Object getOrderist() {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			Integer order = salaryItemService.getOrderList();
			objectNode.putPOJO("response", order);
			objectNode.put("message", "获取显示顺序成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/getCalItems")
	@ResponseBody
	public Object getSalaryItems() {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			List<SalaryItem> salaryItems = salaryItemService
					.getAllParSalaryItems();
			objectNode.putPOJO("response", salaryItems);
			objectNode.put("message", "获取操作项目成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

}
